﻿using Newtonsoft.Json;
using Sl.Upload.FileTest.Models;
using Sl.Upload.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace Sl.Upload.FileTest
{
    public class FileUpload : IHttpHandler
    {
        HttpPostedFile file = null;
        Para para = new Para();
        public bool IsReusable => false;

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.HttpMethod.Equals("OPTIONS"))
            {
                return;
            }
            else if (context.Request.HttpMethod.Equals("POST"))
            {
                file = context.Request.Files.Count > 0 ? context.Request.Files[0] : null;
                para.Command = string.IsNullOrEmpty(context.Request["command"]) ? CommandType.Upload : (CommandType)Enum.Parse(typeof(CommandType), context.Request["command"]);
                CompleteInfo ci = new CompleteInfo();
                para.Id = string.IsNullOrEmpty(context.Request["OnlyId"]) ? 0 : long.Parse(context.Request["OnlyId"]);
                para.UploadPathType = context.Request["UploadPathType"];
                para.UploadType = string.IsNullOrEmpty(context.Request["uploadtype"]) ? UploadType.Auto : (UploadType)Enum.Parse(typeof(UploadType), context.Request["uploadtype"]);
                para.Guid = context.Request["guid"];
                para.Chunks = string.IsNullOrEmpty(context.Request["chunks"]) ? 0 : int.Parse(context.Request["chunks"]);
                para.Chunk = string.IsNullOrEmpty(context.Request["chunk"]) ? 0 : int.Parse(context.Request["chunk"]);
                para.FileName = context.Request["name"];
                JsonSerializer serializer = new JsonSerializer();
                StringReader sr = new StringReader((string.IsNullOrEmpty(context.Request["rule"]) ? "" : context.Request["rule"]));
                List<CutRule> cutRule = serializer.Deserialize<List<CutRule>>(new JsonTextReader(sr));
                Upload upload = new Upload(new StoragePath(), new ThumbanailRule());
                ci = upload.UploadFile(file, para, cutRule);
                string result = string.Empty;
                switch (ci.UploadStatus)
                {
                    case UploadStatus.None:
                        break;
                    case UploadStatus.Suspicious:
                        break;
                    case UploadStatus.Prohibit:
                        break;
                    case UploadStatus.Authority:
                        break;
                    case UploadStatus.Success:
                        result = "{\"name\":\"" + ci.Name + "\",\"url\":\"" + ci.Url + "\",\"size\":\"" + ci.Size + "\",\"updateStart\":0,\"height\":\"" + ci.Height + "\",\"width\":\"" + ci.Width + "\"}";
                        break;
                    case UploadStatus.Error:
                        break;
                    default:
                        break;
                }
                context.Response.Write(result);
            }
        }
    }
}